package com.huawei.codelab.slice;

import com.huawei.codelab.MainAbility;
import com.huawei.codelab.ResourceTable;
import com.huawei.codelab.utils.CommonData;
import com.huawei.codelab.utils.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.ArrayList;
import java.util.List;

import static com.huawei.codelab.utils.CommonData.TAG;

public class TestDistributedSlice extends AbilitySlice implements IAbilityContinuation {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_distributed);
        getDevices();
        findComponentById(ResourceTable.Id_dataTxt).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (devices.size()>0){
//                    Operation operation = new Intent.OperationBuilder().withDeviceId(devices.get(0).getDeviceId())
//                            .withAbilityName(MainAbility.class.getName())
//                            .withAction(CommonData.TEST_DISTRIBUTED)
//                            .build();
//                    Intent startIntent=new Intent();
//                    intent.setOperation(operation);

                    continueAbility(devices.get(0).getDeviceId());
                }
            }
        });

    }
    private List<DeviceInfo> devices = new ArrayList<>();

    private void getDevices() {
        if (devices.size() > 0) {
            devices.clear();
        }
        List<DeviceInfo> deviceInfos =
                DeviceManager.getDeviceList(ohos.distributedschedule.interwork.DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        LogUtil.info(TAG, "MathGameAbilitySlice deviceInfos size is :" + deviceInfos.size());
        devices.addAll(deviceInfos);
    }

    @Override
    public boolean onStartContinuation() {
        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {

    }

    @Override
    public void onRemoteTerminated() {

    }
}
